package com.project.controller;

import com.project.entity.RestPOJO;
import com.project.entity.RoleUser;
import com.project.entity.Users;
import com.project.service.RoleService;
import com.project.service.UserinfoService;
import com.project.tools.OSSTools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


@RestController
@Api(tags="用户管理相关")
public class UserinfoController {

    @Autowired
    private UserinfoService userinfoService;

    @Autowired
    private RoleService roleService;

    @ApiOperation(
            value = "通过用户手机号和密码进行登录",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class,
            notes = "用户登录"
    )
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name="user_telephone",value="用户手机号",required=true),
            @ApiImplicitParam(paramType="query",name="user_password",value="用户密码",required=true),
    })
    @GetMapping("/login.do")
    public RestPOJO login(String user_telephone, String user_password){
        Users loginuser = userinfoService.getUserinfoByPhone(user_telephone);
        RoleUser roleUser = roleService.selUserRoleByUid(loginuser.getUser_id());
        RestPOJO restPOJO = new RestPOJO();
        if (loginuser == null){
            restPOJO.setStatus(1001);
            restPOJO.setMessages("账户不存在");
        } else if(loginuser.getUser_state().equals("禁用")){
            restPOJO.setStatus(1002);
            restPOJO.setMessages("账户已冻结");
        } else if (!user_password.equals(loginuser.getUser_password())){
            restPOJO.setStatus(1003);
            restPOJO.setMessages("密码错误");
        } else if(roleUser == null){
            restPOJO.setStatus(1004);
            restPOJO.setMessages("未选择角色");
            restPOJO.setData(loginuser);
        }
        else{
            restPOJO.setStatus(1000);
            restPOJO.setMessages("查询成功");
            restPOJO.setData(loginuser);
        }
        return restPOJO;
    }

    @ApiOperation(
            value = "通过用户名和手机号验证用户信息",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class,
            notes = "用户信息验证"
    )
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name="user_telephone",value="用户手机号",required=true),
            @ApiImplicitParam(paramType="query",name="user_name",value="用户姓名",required=true),
    })
    @GetMapping("/verify")
    public RestPOJO forget(String user_telephone, String user_name){
        Users user = userinfoService.getUserinfoByPhone(user_telephone);
        RestPOJO restPOJO = new RestPOJO();
        if (user == null){
            restPOJO.setStatus(1001);
            restPOJO.setMessages("账户不存在");
        } else if(user.getUser_state().equals("禁用")){
            restPOJO.setStatus(1002);
            restPOJO.setMessages("账户已冻结");
        } else if (!user_name.equals(user.getUser_name())){
            restPOJO.setStatus(1003);
            restPOJO.setMessages("用户名错误");
        }
        else{
            restPOJO.setStatus(1000);
            restPOJO.setMessages("查询成功");
            restPOJO.setData(user);
        }
        return restPOJO;
    }

    @ApiOperation(
            value = "通过用户ID查询单个用户信息",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class,
            notes = "查询单个用户信息"
    )
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="path",name="id",value="用户的用户id",required=true)
    })
    @GetMapping("/userinfo/{id}")
    public RestPOJO selUserById(@PathVariable Integer id){
        RestPOJO rst = new RestPOJO();
        Users user = userinfoService.getUserById(id);
        if(user != null){
            rst.setData(user);
            rst.setMessages("查询成功");
            rst.setStatus(1000);
        }
        else{
            rst.setMessages("查询失败");
            rst.setStatus(1001);
        }
        return rst;
    }

    @ApiOperation(
            value = "修改用户头像",
            httpMethod = "POST",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class,
            notes = "修改头像"
    )
    @PostMapping("/userinfo/photo")
    public RestPOJO updUserPhoto(Users users, MultipartFile headimg1){
        String photo= OSSTools.upLoad(System.currentTimeMillis()+"",headimg1);
        users.setUser_photo(photo);
        int a = userinfoService.updUserPhoto(users);
        RestPOJO rest = new RestPOJO();
        if (a > 0){
            rest.setStatus(1000);
            rest.setMessages("头像修改成功");
        }
        else {
            rest.setStatus(1001);
            rest.setMessages("头像修改失败");
        }
        return rest;
    }

    @ApiOperation(
            value = "修改用户个人信息",
            httpMethod = "PUT",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class,
            notes = "修改个人信息"
    )
    @PutMapping("/userinfo")
    public RestPOJO updateUserinfo(@RequestBody Users users){
        int a = userinfoService.updateUserinfo(users);
        RestPOJO rest = new RestPOJO();
        if (a > 0){
            rest.setStatus(1000);
            rest.setMessages("修改成功");
        }
        else {
            rest.setStatus(1001);
            rest.setMessages("修改失败");
        }
        return rest;
    }

    @ApiOperation(
            value = "注册用户",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    //方法的参数说明

    @PostMapping("/Reset.do")
    public RestPOJO addUserinfo(@RequestBody Users users){
        Integer a = userinfoService.addUserinfo(users);
        RestPOJO rest = new RestPOJO();
        if (a > 0){
            rest.setStatus(1000);
            rest.setMessages("注册成功");
        }
        else {
            rest.setStatus(1001);
            rest.setMessages("注册失败");
        }
        return rest;
    }

    @ApiOperation(
            value = "根据用户电话号码查用户id",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    //方法的参数说明
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="path",name="user_telephone",value="电话号码",required=true)
    })
    @GetMapping("/getUserId/{user_telephone}")
    public RestPOJO login(@PathVariable("user_telephone") String user_telephone){
        Users loginuser = userinfoService.getUserinfoByPhone(user_telephone);
        RestPOJO restPOJO = new RestPOJO();
        if (loginuser == null){
            restPOJO.setStatus(1001);
            restPOJO.setMessages("账户不存在");
        } else{
            restPOJO.setStatus(1000);
            restPOJO.setMessages("查询成功");
            restPOJO.setData(loginuser);
        }
        return restPOJO;
    }

    @ApiOperation(
            value = "查电话号码是否注册过",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    //方法的参数说明
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="path",name="user_telephone",value="电话号码",required=true)
    })
    @GetMapping("/verifyTelephone/{user_telephone}")
    public RestPOJO verifyTelephone(@PathVariable("user_telephone") String user_telephone){
        Users loginuser = userinfoService.getUserinfoByPhone(user_telephone);
        RestPOJO restPOJO = new RestPOJO();
        if (loginuser == null){
            restPOJO.setStatus(1000);
            restPOJO.setMessages("手机号可以使用");
        } else{
            restPOJO.setStatus(1001);
            restPOJO.setMessages("手机号已经被注册");
            restPOJO.setData(loginuser);
        }
        return restPOJO;
    }

    @ApiOperation(
            value = "修改用户密码",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class,
            notes = "修改用户密码"
    )
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name="user_id",value="用户ID",required=true),
            @ApiImplicitParam(paramType="query",name="user_password",value="用户密码",required=true),
    })
    @GetMapping("/password")
    public RestPOJO updatePassword(Integer user_id, String user_password){
        int a = userinfoService.updatePassword(user_id, user_password);
        RestPOJO rest = new RestPOJO();
        if (a > 0){
            rest.setStatus(1000);

            rest.setMessages("修改成功");
        }
        else {
            rest.setStatus(1001);
            rest.setMessages("修改失败");
        }
        return rest;
    }

    @GetMapping("/deleteUserinfo/{user_id}")
    public RestPOJO deleteTelephone(@PathVariable("user_id") Integer user_id){
        RestPOJO rest = new RestPOJO();
        Integer a = userinfoService.deleteUserinfo(user_id);
        if (a > 0){
            rest.setStatus(1000);
            rest.setMessages("删除成功");
        }
        else {
            rest.setStatus(1001);
            rest.setMessages("删除失败");
        }
        return rest;
    }
}
